package com.example.controller.admin;


import com.example.dto.CategoryDTO;
import com.example.result.PageBeanResult;
import com.example.result.Result;
import com.example.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Tag(name = "分类管理")
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分页展示所有分类
     */
    @GetMapping("/pages")
    @Operation(summary = "分页展示所有分类")
    public Result showCategory(@RequestParam(defaultValue = "0") Integer page,
                               @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("分页展示分类：{},{}",page,pageSize);
        PageBeanResult pageBeanResult=categoryService.showCategory(page,pageSize);
        return Result.success(pageBeanResult);

    }
    /**
     * 按名称查询并分页展示
     */
    @GetMapping("/page")
    @Operation(summary = "按名称查询并分页展示")
    public Result selectNameCategory(String name,
                                     @RequestParam(defaultValue = "0") Integer page,
                                     @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("按名字查询分类并分页展示:{},{},{}",name,page,pageSize);
        PageBeanResult pageBeanResult=categoryService.selectNameCategory(name,page,pageSize);
        return  Result.success(pageBeanResult);
    }

    /**
     * 启用禁用分类
     */
    @PostMapping("/status/{status}")
    @Operation(summary = "启用禁用分类")
    public Result starOrStop(@PathVariable Integer status ,Integer id){
        log.info("启用，禁用分类：{},{}",status,id);
        categoryService.starOrStop(status,id);
        return  Result.success();
    }

    /**
     * 删除分类
     */
    @DeleteMapping
    @Operation(summary = "根据ID删除分类")
    public Result deleteCategory(Integer id){
        log.info("根据id删除分类,{}",id);
        categoryService.deleteCategory(id);
        return  Result.success();

    }

    /**
     * 批量删除分类
     */
    @DeleteMapping("/categories/{ids}")
    @Operation(summary = "批量删除分类")
    public Result deleteCategories(@PathVariable List<Integer> ids){
        categoryService.deleteCategories(ids);
        return Result.success();
    }
    /**
     * 新增分类
     */
    @PostMapping
    @Operation(summary = "新增分类")
    public Result addCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.addCategory(categoryDTO);
        return Result.success();
    }
    /**
     * 数据回显
     */
    @GetMapping ("/{id}")
    @Operation(summary = "数据回显")
    public Result selectCategory(@PathVariable Integer id){
        CategoryDTO categoryDTO=categoryService.selectCategory(id);
        return Result.success(categoryDTO);
    }
    /**
     * 修改分类
     */
    @PutMapping
    @Operation(summary = "修改分类")
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 查询分类
     * @param
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "根据类型查询分类")
    public Result list(){
        List<CategoryDTO> list = categoryService.list();
        return Result.success(list);
    }

}
